<template>
	<view class="login">
		<button type="primary" v-if="sqFlag" @tap="wxMandate">微信授权</button>
		<button type="primary" v-if="phoneFlag" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</button>
		<view class="agreement">
			登录即代表您同意
			<text>用户协议</text>
			和
			<text>隐私协议</text>
		</view>
	</view>
</template>

<script>
import { login } from '@/api/index.js';
export default {
	onLoad(options) {
		// console.log(options);
		if (options.redirect) {
			// console.log('onload');
			this.redirect = decodeURIComponent(options.redirect);
		}
		// console.log(this.redirect)
	},
	data() {
		return {
			sqFlag: true,
			phoneFlag: false,
			userInfo: null,
			redirect: null
		};
	},
	methods: {
		// 微信授权
		wxMandate() {
			uni.getUserProfile({
				desc: '用于用户登录',
				success: res => {
					// console.log(res);
					this.userInfo = res;

					login
						.wxLogin({
							openid: uni.getStorageSync('openid'),
							iv: res.iv,
							rawData: res.rawData,
							signature: res.signature,
							encryptedData: res.encryptedData
						})
						.then(res => {
							console.log(res, 'res');
							if (res.data.message == '失败') {
								// new Error('没有注册手机号')
								// console.log('失败了')
								this.sqFlag = false;
								this.phoneFlag = true;
								uni.showToast({
									title: '请获取手机号',
									duration: 2000,
									icon: 'none'
								});
								return;
							}
							// 成功就跳转页面,如果有指定的跳转的页面redirect 就跳到该页面
							uni.setStorageSync('token', res.data.data.token);
							console.log(this.userInfo);
							uni.setStorageSync('userInfo', this.userInfo.userInfo);
							// console.log(this.redirect, 'REDIRECT');
							if (this.redirect) {
								// debugger;
								uni.navigateTo({
									url: this.redirect
								});
							} else {
								// debugger;
								// 如果没有跳转的页面就返回上一页
								console.log('返回上个页面');
								uni.navigateBack({
									delta: 0
								});
							}
						})
						.catch(e => {
							console.log('登陆失败，因为没有注册手机号');
							console.log(e);
							// 失败的话就是没有获取到手机号，就跳转到注册手机号
						});
				},
				fail: e => {
					console.log(e);
					uni.showToast({
						title: '授权失败',
						duration: 2000,
						icon: 'none'
					});
				}
			});
		},
		getPhoneNumber(e) {
			console.log(this.userInfo);
			// console.log(e);
			let data = e.detail;
			login
				.mobileLogin({
					openid: uni.getStorageSync('openid'),
					iv: data.iv,
					encryptedData: data.encryptedData,
					userInfo: {
						rawData: this.userInfo.rawData,
						signature: this.userInfo.signature,
						encryptedData: this.userInfo.encryptedData,
						iv: this.userInfo.iv
					}
				})
				.then(res => {
					console.log(res, '成功的');
					// uni.setStorageSync('token', res.data.data.token);
					// uni.setStorageSync('userInfo', this.userInfo);
					uni.setStorageSync('userInfo', this.userInfo.userInfo);
					if (this.redirect) {
						// debugger;
						uni.navigateTo({
							url: this.redirect
						});
					} else {
						// debugger;err
						// 如果没有跳转的页面就返回上一页
						// console.log('返回上个页面');
						uni.navigateBack({
							delta: 0
						});
					}
				})
				.catch(e => {
					console.log(e, 'err');
				});
		}
	}
};
</script>

<style lang="less">
.login {
	padding: 0 30rpx;
	button {
		color: white;
		margin-top: 200rpx;
		margin-bottom: 30rpx;
		font-size: 38rpx;
		background-color: #0b76de;
	}

	.agreement {
		font-size: 30rpx;
		color: #3b4144;
		text {
			color: blue;
		}
	}
}
</style>
